package com.cff.websocket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

public class WebSocketUser {
	private static Map<String,List<WebSocketSession>> nickNameWebsession = null;
	private static Map<WebSocketSession,String> webSessionNickName = null;
	private static Map<String,List<WebSocketSession>> userIdWebsession = null;
	private static Map<WebSocketSession,String> webSessionUserId = null;
	private static Map<String,List<TextMessage>> nickNameWithOfflineMsg = null;
	private static long websocketId = 100000000L;
	
	public WebSocketUser(){
		nickNameWebsession = new HashMap<String,List<WebSocketSession>>();
		webSessionNickName = new HashMap<WebSocketSession,String>();
		nickNameWithOfflineMsg = new HashMap<String,List<TextMessage>>();
		userIdWebsession = new HashMap<String,List<WebSocketSession>>();
		webSessionUserId = new HashMap<WebSocketSession,String>();
	}
	
	public Map<String,List<WebSocketSession>> getNickNameWebsession(){
		return nickNameWebsession;
	}
	
	public Map<WebSocketSession,String> getWebSessionNickName(){
		return webSessionNickName;
	}
	
	public String getNickWithWebSocketSession(WebSocketSession webSocketSession){
		return webSessionNickName.get(webSocketSession); 
	}

	
	public static Map<String, List<WebSocketSession>> getUserIdWebsession() {
		return userIdWebsession;
	}

	public static void setUserIdWebsession(Map<String, List<WebSocketSession>> userIdWebsession) {
		WebSocketUser.userIdWebsession = userIdWebsession;
	}

	public static Map<WebSocketSession, String> getWebSessionUserId() {
		return webSessionUserId;
	}

	public static void setWebSessionUserId(Map<WebSocketSession, String> webSessionUserId) {
		WebSocketUser.webSessionUserId = webSessionUserId;
	}

	public static Map<String, List<TextMessage>> getNickNameWithOfflineMsg() {
		return nickNameWithOfflineMsg;
	}

	public static void setNickNameWithOfflineMsg(Map<String, List<TextMessage>> nickNameWithOfflineMsg) {
		WebSocketUser.nickNameWithOfflineMsg = nickNameWithOfflineMsg;
	}

	public static void setNickNameWebsession(Map<String, List<WebSocketSession>> nickNameWebsession) {
		WebSocketUser.nickNameWebsession = nickNameWebsession;
	}

	public static void setWebSessionNickName(Map<WebSocketSession, String> webSessionNickName) {
		WebSocketUser.webSessionNickName = webSessionNickName;
	}

	/**
	 * 根据昵称拿WebSocketSession
	 * @param nickName
	 * @return
	 */
	public List<WebSocketSession> getWebSocketSessionWithNickName(String nickName){
		return nickNameWebsession.get(nickName);
	}
	
	/**
	 * 存放昵称和WebSocketSession
	 * @param nickName
	 * @param webSocketSession
	 */
	public synchronized void putNickNameAndWebSocketSession(String nickName, WebSocketSession webSocketSession){
		webSessionNickName.put(webSocketSession, nickName);
		if (nickNameWebsession.get(nickName) == null){
			List<WebSocketSession> webSessoin = new ArrayList<WebSocketSession>();
			webSessoin.add(webSocketSession);
			nickNameWebsession.put(nickName, webSessoin);
		}else{
			List<WebSocketSession> webSessoin = nickNameWebsession.get(nickName);
			webSessoin.add(webSocketSession);
		}
	}
	
	/**
	 * 移除失效的WebSocketSession
	 * @param webSocketSession
	 */
	public synchronized void removeWebSocketSession(WebSocketSession webSocketSession){
		if (webSocketSession == null) return;
		String nickName = webSessionNickName.get(webSocketSession);
		webSessionNickName.remove(webSocketSession);
		List<WebSocketSession> webSessoin = nickNameWebsession.get(nickName);
		if (webSessoin == null) return;
		webSessoin.remove(webSocketSession);
	}
	
	/**
	 * 移除无用的WebSocketSession
	 * @param webSocketSession
	 */
	public synchronized void removeWebSocketSession(String UserId,WebSocketSession webSocketSession){
		if (webSocketSession == null) return;

		List<WebSocketSession> webSessoin = userIdWebsession.get(UserId);
		if (webSessoin == null) return;
		webSessoin.remove(webSocketSession);
	}
	
	/**
	 * 存放离线消息
	 * @param nickName
	 * @param msg
	 */
	public synchronized void putOfflineMsg(String nickName, TextMessage msg){
		if (nickNameWithOfflineMsg.get(nickName) == null){
			List<TextMessage> msgList = new ArrayList<TextMessage>();
			msgList.add(msg);
			nickNameWithOfflineMsg.put(nickName, msgList);
		}else{
			List<TextMessage> msgList = nickNameWithOfflineMsg.get(nickName);
			msgList.add(msg);
		}
	}
	
	/**
	 * 根据昵称拿离线消息
	 * @param nickName
	 * @return
	 */
	public List<TextMessage> getOfflineMsgWithNickName(String nickName){
		return nickNameWithOfflineMsg.get(nickName);
	}
	
	/**
	 * 根据userId拿WebSocketSession
	 * @param nickName
	 * @return
	 */
	public List<WebSocketSession> getWebSocketSessionWithUserId(String userId){
		return userIdWebsession.get(userId);
	}
	
	/**
	 * 存放昵称和WebSocketSession
	 * @param nickName
	 * @param webSocketSession
	 */
	public synchronized void putUserIdAndWebSocketSession(String userId, WebSocketSession webSocketSession){
		webSessionUserId.put(webSocketSession, userId);
		if (userIdWebsession.get(userId) == null){
			List<WebSocketSession> webSessoin = new ArrayList<WebSocketSession>();
			webSessoin.add(webSocketSession);
			userIdWebsession.put(userId, webSessoin);
		}else{
			List<WebSocketSession> webSessoin = userIdWebsession.get(userId);
			webSessoin.add(webSocketSession);
		}
	}
	
	/**
	 * 移除失效的WebSocketSession
	 * @param webSocketSession
	 */
	public synchronized long getWebsocketId(){
		websocketId += 1;
		return websocketId;
	}
	
	public static void main(String srga[]){
		String test = "123456";
		
		Boolean flag = true;
		if(test == "012345678".substring(1, 7)){
			flag = false;
		}
		System.out.println(flag);
	}
}
